#!/bin/bash
Get_NodeInit_Status(){
    echo "############################ 收集节点初始化信息 #############################"

    # CheckPoint 1 cloud-init 安装查看
    cloud-init -h |grep init > /dev/null
    cloud_init_exec_result=$?

    if [ $cloud_init_exec_result -eq 0 ];then
      echo "INFO: cloud-init安装正常。"
    else
      echo "ERROR: cloud-init安装异常。"
    fi

    # CheckPoint 2 cloud-init初始化状态查看
    if [ ! -f "/run/cloud-init/status.json" ];then
      echo "ERROR: 获取cloud-init执行状态失败。"
    else
      echo "INFO: 获取cloud-init执行状态成功。"
    fi

    # CheckPoint 3 agent下载查看
    if [ ! -f "/usr/local/qcloud/qdocker_agent/bin/container_cluster_agent" ];then
      echo "ERROR: container_cluster_agent安装失败。"
    else
      echo "INFO: container_cluster_agent安装成功。"
    fi

    # CheckPoint 4 agent进程查看
    if [ $(ps axu | grep container_cluster_agent | grep -v grep | wc -l) -ge 1 ];then
        echo "INFO: container_cluster_agent进程运行中"
    else
        echo "ERROR: container_cluster_agent进程未运行"
    fi

    # CheckPoint 6 检查crt下载文件
    crtCnt=$(find /usr/local/qcloud/qdocker_agent/crt/* |wc -l )
     if [ $crtCnt -eq 3 ];then
      echo "INFO: crt 文件下载正常。"
    else
      echo "ERROR: crt 文件下载异常。"
    fi

    # CheckPoint 7 静态域名解析
    parsedIp=$(dig static.ccs.tencentyun.com |grep 'ANSWER SECTION' -1 |grep 'static' |awk '{print$5}')

    if [ -n "$parsedIp" ];then
      echo "INFO: static.ccs.tencentyun.com域名解析正常: $parsedIp"
    else
      echo "ERROR: static.ccs.tencentyun.com域名解析异常。"
    fi

    # CheckPoint 8 检查dockerd服务
    dockerSvcCnt=$(systemctl list-units --type=service --state=running --no-pager  |grep dockerd|wc -l)
    if [ $dockerSvcCnt -eq 1 ];then
      echo "INFO: docker服务正常。"
    else
      echo "ERROR: docker服务异常。"
    fi

    # CheckPoint 9 检查kubelet服务
    kubeletSvcCnt=$(systemctl list-units --type=service --state=running --no-pager  |grep kubelet|wc -l)
    if [ $kubeletSvcCnt -eq 1 ];then
      echo "INFO: kubelet服务正常。"
    else
      echo "ERROR: kubelet服务异常。"
    fi

    return 0
}

Get_NodeInit_Status